import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.prefs.Preferences;
import javax.swing.*;
import simplex3d.console.findreplace.*;

// Add rsyntaxtextarea.jar to your classpath to try this example.
import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;


/**
 *
 * @author Aleksey Nikiforov (lex)
 */
public class RSyntaxTestFrame extends javax.swing.JFrame {

    private String sampleText;


    /** Creates new form Frame */
    public RSyntaxTestFrame() {
        initComponents();

        sampleText = loadFile("sample.text");
        Preferences prefs = Preferences.userNodeForPackage(this.getClass());

        // Temporary prefs if you have no permissions or dont want to save history and settings.
        //Preferences prefs = TempPreferences.userNodeForPackage(this.getClass());


        // Using RSyntaxTextArea
        RSyntaxTextArea rTextArea = new RSyntaxTextArea();
        FindReplacePanel findReplace = new FindReplacePanel(
            prefs,
            rTextArea, new RTextScrollPane(),
            JComponent.WHEN_IN_FOCUSED_WINDOW
        );

        // Best antialiasing available, but doesnt work well on windows xp.
        rTextArea.setTextAntiAliasHint("VALUE_TEXT_ANTIALIAS_LCD_HRGB");

        // Optionally change the font
        //textArea.setFont(new java.awt.Font(java.awt.Font.MONOSPACED, java.awt.Font.PLAIN, 12));

        rTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        rTextArea.setTabsEmulated(true);
        rTextArea.setTabSize(4);

        // MAKING UNDO WORK: setup forwards.
        findReplace.setUndoForwards(
            RSyntaxTextArea.getAction(RSyntaxTextArea.UNDO_ACTION),
            RSyntaxTextArea.getAction(RSyntaxTextArea.REDO_ACTION)
        );

        // MAKING UNDO WORK: replace component's undo menu entries.
        JPopupMenu menu = rTextArea.getPopupMenu();
        menu.remove(0);
        menu.remove(0);
        menu.insert(findReplace.getRedoAction(), 0);
        menu.insert(findReplace.getUndoAction(), 0);
        menu.addSeparator();
        menu.add(new JMenuItem(findReplace.getShowFindDialogAction()));
        menu.add(new JMenuItem(findReplace.getShowReplaceDialogAction()));

        // MAKING UNDO WORK: remove component's undo keys.
        InputMap map = (InputMap) UIManager.get("RSyntaxTextAreaUI.inputMap");
        int mods = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        map.remove(KeyStroke.getKeyStroke(KeyEvent.VK_Z, mods));
        map.remove(KeyStroke.getKeyStroke(KeyEvent.VK_Y, mods));

        addTab(findReplace);

        // Fixes undo/redo context menu for RSyntaxTextArea.
        findReplace.getTextComponent().requestFocus();
    }

    private void addTab(FindReplacePanel panel) {
        tabbedPanel.addTab(panel.getTextComponent().getClass().getSimpleName(), panel);
        panel.getTextComponent().setText(sampleText);
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPanel = new javax.swing.JTabbedPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
        );

        tabbedPanel.getAccessibleContext().setAccessibleName("Editor1");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        //-Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {}

        //-Duser.language=ru -Duser.country=RU
        //java.util.Locale.setDefault(new java.util.Locale("ru", "RU"));

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RSyntaxTestFrame().setVisible(true);
            }
        });
    }

    private static String loadFile(String name) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(name)));
            StringBuilder sb = new StringBuilder();

            String line; while ((line = in.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }

            return sb.toString();
        }
        catch (IOException ex) {
            return "";
        }
        finally {
            if (in != null) try {
                in.close();
            } catch (IOException ignore) {}
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabbedPanel;
    // End of variables declaration//GEN-END:variables

}
